/*** vender ***/
package util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.image.BufferedImage;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Chart {

  /**
   * Cria um gráfico composto das vendas
   * @param seriesA serie de dados em valores absolutos
   * @param seriesT serie de dados em reais
   * @return a imagem do grafico
   */
  public static BufferedImage getSalesCompositeChart(XYSeries seriesA, XYSeries seriesT, int width, int height) {

    XYDataset dataSetAmount = new XYSeriesCollection(seriesA);
    XYDataset dataSetTotal = new XYSeriesCollection(seriesT);

    // create subplot
    final XYSplineRenderer renderer = new XYSplineRenderer(1);
    renderer.setSeriesPaint(0, Color.RED);
    renderer.setSeriesStroke(0, new BasicStroke(1.5f));

    XYSplineRenderer renderer2 = new XYSplineRenderer(1);
    renderer2.setSeriesPaint(0, Color.BLUE);
    renderer2.setSeriesStroke(0, new BasicStroke(1.5f));

    final NumberAxis rangeAxis1 = new NumberAxis("Vendas absolutas");
    final XYPlot subplot = new XYPlot(dataSetAmount, null, rangeAxis1, renderer);
    subplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

    // add secondary axis
    subplot.setDataset(1, dataSetTotal);
    final NumberAxis axis2 = new NumberAxis("Vendas em reais");
    axis2.setAutoRangeIncludesZero(false);
    subplot.setRangeAxis(1, axis2);
    subplot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
    subplot.setRenderer(1, renderer2);
    subplot.mapDatasetToRangeAxis(1, 1);

    // parent plot
    final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain"));
    plot.setGap(10.0);

    // add the subplot
    plot.add(subplot, 1);
    plot.setOrientation(PlotOrientation.VERTICAL);
    // change the auto tick unit selection to integer units only...
    final NumberAxis domainAxis = new NumberAxis("Dias");
    domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    plot.setDomainAxis(domainAxis);
    plot.setBackgroundPaint(Color.WHITE);

    JFreeChart chart = new JFreeChart("Vendas", plot);
    chart.setBackgroundPaint(Color.WHITE);

    return chart.createBufferedImage(width, height);
  }

  public static BufferedImage getSalesChart(XYSeries series, int width, int height) {

    XYDataset xyDataset = new XYSeriesCollection(series);

    final JFreeChart chart = ChartFactory.createXYLineChart(
            "Vendas",
            "Dia",
            "Vendas",
            xyDataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false);


    XYPlot plot = chart.getXYPlot();
    final NumberAxis domainAxis = new NumberAxis("Dias");
    domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    plot.setDomainAxis(domainAxis);
    plot.setRenderer(new XYSplineRenderer());


    return chart.createBufferedImage(width, height);
  }

  public static BufferedImage getReqChart(XYSeries series, int width, int height) {

    XYDataset xyDataset = new XYSeriesCollection(series);

    final JFreeChart chart = ChartFactory.createXYLineChart(
            "Requisições",
            "Dias",
            "Requisições",
            xyDataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false);


    XYPlot plot = chart.getXYPlot();
    XYSplineRenderer renderer = new XYSplineRenderer();
    renderer.setSeriesPaint(0, Color.BLUE);
    final NumberAxis domainAxis = new NumberAxis("Dias");
    domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    plot.setDomainAxis(domainAxis);
    plot.setRenderer(renderer);


    return chart.createBufferedImage(width, height);
  }
}
